'use strict';

angular.module('com.dcos.app').controller('LoginController', function LoginController($rootScope, $state, $q, $window,$cookies, LoginService) {
  var vm = this;
  $rootScope.loading = false;
  $cookies.remove("token");
  var deferred = $q.defer();

  vm.username = $cookies.get("preservedUsername");
  vm.password = $cookies.get("preservedPassword");
  if(vm.username && vm.username != ""){
    vm.isPreserved = true;
  }else{
    vm.isPreserved = false;
  }

  vm.submitted = false;

  vm.login = function () {
    if(!vm.username || !vm.password){
      return;
    }
    LoginService.login(vm.username, vm.password).then(function (response) {
      $window.sessionStorage.setItem("curUserId",response.data.userid);
      $window.sessionStorage.setItem("curUserType",response.data.usertype);
      $window.sessionStorage.setItem("curDept_Id",response.data.dept_id);
      $window.sessionStorage.setItem("curUserId",response.data.userid);
      $window.sessionStorage.setItem("curUserName",vm.username);
      $state.go("projectcenter");
    }, function(){
      vm.submitted = true;
      vm.error = true;
      deferred.reject();
      return deferred.promise;
    });
    /*
    if(!vm.username || !vm.password){
      return;
    }
    vm.submitted = true;
    vm.logining = true;

    if(vm.isPreserved){
      $cookies.put("preservedUsername", vm.username);
      $cookies.put("preservedPassword", vm.password);
    }else{
      $cookies.remove("preservedUsername");
      $cookies.remove("preservedPassword");
    }

    LoginService.login(vm.username, vm.password).then(function (response) {
       var userId = response.data.token.user.id;
       vm.unscopedTokenId = response.headers('x-subject-token');
       return LoginService.getUserProjects(userId, vm.unscopedTokenId);
    }, function(){
      vm.authError = false;
      vm.error = true;
      deferred.reject();
      return deferred.promise;
    }).then(function (response) {
      // 若用户没有分配权限
      if(response.data.projects.length === 0){
        vm.error = false;
        vm.authError = true;
        deferred.reject();
        return deferred.promise;
      }
      return LoginService.getTokens(response.data.projects[0].id, vm.unscopedTokenId);
    }).then(function (response) {
      var tokenId = response.headers('x-subject-token');
      var token = response.data.token;
      token.id = tokenId;
      // 删除token中多余属性，防止cookies过大导致token存储失败
      delete token.catalog;
      delete token.audit_ids;

      $cookies.putObject("token", token);
      $cookies.put("dcos-acs-auth-cookie", tokenId);
      $cookies.put("loggedIn", true);
      LoginService.login_log(vm.username,tokenId).then({});
      $state.go("app.resource.node");
    }, function () {
      vm.authError = false;
      vm.error = true;
      deferred.reject();
      return deferred.promise;
    }).catch(function (e) {
      vm.logining = false;
    });*/
  }
});
